package org.geektimes.jolokia.demo;

import org.jolokia.client.J4pClient;
import org.jolokia.client.J4pClientBuilder;
import org.jolokia.client.exception.J4pException;
import org.jolokia.client.request.*;

import javax.management.ObjectName;
import java.util.Map;

/**
 * @author :   chuan
 * @date :   2021/03/16
 */
public class JolokiaDemo {

    public static void main(String[] args) throws Exception {
        J4pClient j4pClient = getJ4pClient();

//        tomcatMemoryUsed(j4pClient);
//        jolokiaConfig(j4pClient);
//        tomcatCatalinaConfig(j4pClient);

        // homework ... start
        J4pReadRequest readRequest = new J4pReadRequest("org.geektimes.projects.user.management:type=HomeworkManager");

        System.out.println(">>> Get ObjectName -----------------------");
        homeworkForGetObjectName(readRequest);
        System.out.println("-----------------------<<<");

        System.out.println(">>> Get Version -----------------------");
        J4pVersionRequest versionRequest = new J4pVersionRequest();
        HomeworkForVersion(j4pClient, versionRequest);
        System.out.println("-----------------------<<<");

        System.out.println(">>> Get Field -----------------------");
        homeworkForGetField(j4pClient, readRequest);
        System.out.println("-----------------------<<<");

        System.out.println(">>> Operation Default Field Value-----------------------");
        J4pExecRequest execRequest = new J4pExecRequest(readRequest.getObjectName(), "gotoVisitSite");
        HomeworkForOperation(j4pClient, execRequest);
        System.out.println("-----------------------<<<");

        System.out.println(">>> Operation New Field Value-----------------------");
        /*
         *  pAttribute: 参数要大写开头
         */
        J4pWriteRequest writeRequest = new J4pWriteRequest(readRequest.getObjectName(), "VisitUrl", "https://gitee.com/geektime-geekbang");
        HomeworkForOperation(j4pClient, writeRequest);

        J4pExecRequest execRequest2 = new J4pExecRequest(readRequest.getObjectName(), "gotoVisitSite");
        HomeworkForOperation(j4pClient, execRequest2);
        System.out.println("-----------------------<<<");

        // homework ... end
    }

    private static void HomeworkForOperation(J4pClient j4pClient, J4pRequest request) throws Exception {
        j4pClient.execute(request);
    }

    private static void homeworkForGetField(J4pClient j4pClient, J4pReadRequest request) throws J4pException {
        J4pReadResponse response = j4pClient.execute(request);

        Map value = response.getValue();

        for (Object key : value.keySet()) {
            System.out.println(key.toString() + ": " + value.get(key.toString()));
        }
    }

    private static void HomeworkForVersion(J4pClient j4pClient, J4pVersionRequest versionRequest) throws J4pException {
        J4pVersionResponse response = j4pClient.execute(versionRequest);

        System.out.println(response.getValue().toString());
    }

    private static void homeworkForGetObjectName(J4pReadRequest request) {
        ObjectName objectName = request.getObjectName();
        System.out.println("MBean objectName : " + objectName);
    }

    private static void tomcatCatalinaConfig(J4pClient j4pClient) throws Exception {
        J4pReadRequest readRequest = new J4pReadRequest("Catalina:type=ProtocolHandler,port=8080");

        J4pReadResponse response = j4pClient.execute(readRequest);

        Map<String, Object> value = response.getValue();

        for (String key : value.keySet()) {
            System.out.println( key + " :" + value.get(key));
        }
    }

    private static void jolokiaConfig(J4pClient j4pClient) throws Exception {
        J4pReadRequest readRequest = new J4pReadRequest("jolokia:type=Config", "MaxDebugEntries");

        J4pReadResponse response = j4pClient.execute(readRequest);

        System.out.println((Long) response.getValue());
    }

    private static void tomcatMemoryUsed(J4pClient j4pClient) throws Exception {
        J4pReadRequest req = new J4pReadRequest("java.lang:type=Memory","HeapMemoryUsage");

        /* 赋值 */
        req.setPath("used");

        J4pReadResponse response = j4pClient.execute(req);
        Map<String,Long> values = response.getValue();

        long used = values.get("used");
        long max = values.get("max");

        int usage = (int) (used * 100 / max);
        System.out.println("Memory usage: used: " + used + " / max: " + max + " = " + usage + "%");
        System.out.println(values);
    }

    private static J4pClient getJ4pClient() {
        return new J4pClientBuilder()
                .user("jolokia").password("666666")
                .contentCharset("utf-8")
                .maxTotalConnections(20)
                .pooledConnections()
                .socketBufferSize(8192)
                .url("http://localhost:8080/jolokia")
                .build();
    }


}
